package com.example.pixelyear.ui.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import androidx.annotation.Nullable;

import com.example.pixelyear.R;
import com.example.pixelyear.sql.RecordSqlUtils;
import com.example.pixelyear.utils.ActivityListUtils;
import com.example.pixelyear.utils.UIUtils;
import com.example.pixelyear.view.MyProgressLoading;

/**
 * Package    : com.example.baseutils.BaseUI
 * When I wrote this, only The jade emperor and I understood what I was doing, Now God only knows
 * ClassName  : BaseActivity
 * Description: ${TODO}
 * Author     : 坐怀灬不乱
 * Date       : 2018/12/22 14:59
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected BaseActivity mActivity;
    protected MyProgressLoading loading;
    protected ActivityListUtils lifeUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            fixOrientation();
        }
        super.onCreate(savedInstanceState);
        if (getLayoutResID() != 0)
            setContentView(getLayoutResID());
        lifeUtils = ActivityListUtils.getInstance();
        lifeUtils.addActivity(this);
        mActivity = this;
        loading = new MyProgressLoading(mActivity, R.style.DialogStyle);
        FirstLoad(savedInstanceState);
        setTitleBarColor();
        initView(savedInstanceState);
        initData();
        initListener();
    }

    //使子模块功能 能优先于 initView initData initListener加载
    protected void FirstLoad(Bundle savedInstanceState) {
        setStateBarTextColor(true);
    }

    protected void setTitleBarColor() {
    }

    /**
     * @param isLight
     */
    protected void setStateBarTextColor(boolean isLight) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            setTranslucentStatus(true);
        } else {
            setTranslucentStatus(false);
        }
        //        //亮色状态栏字体颜色为暗色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //        官方在Android6.0中提供了亮色状态栏模式，配置只需一行代码
            UIUtils.setStatusBar(this, 1, R.color.transparent);
            if (setIsNeedBlackStateBar() && isLight)
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            UIUtils.setStatusBar(this, 3, R.color.black_b7b7b7);
        }
    }

    /**
     * 布局view
     */
    public abstract int getLayoutResID();

    /**
     * 初始化控件
     */
    public abstract void initView(@Nullable Bundle bundle);

    /**
     * 初始化数据
     */
    public abstract void initData();

    /**
     * 初始化监听
     */
    public abstract void initListener();

    public boolean setIsNeedBlackStateBar() {
        return true;
    }

    @TargetApi(19)
    protected void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    private boolean fixOrientation() {
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo) field.get(this);
            o.screenOrientation = -1;
            field.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //修复8.0 Only Fullscreen activities can change request orientation问题
    protected boolean isTranslucentOrFloating() {
        boolean isTranslucentOrFloating = false;
        try {
            int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean) m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }

    @Override
    protected void onDestroy() {
        if (RecordSqlUtils.Companion.getInstance(mActivity) != null)
            RecordSqlUtils.Companion.getInstance(mActivity).close();
        if (loading.isShowing())
            loading.dismiss();
        lifeUtils.removeActivity(this);
        super.onDestroy();
    }
}
